package com.flaskpy.web.eternity.admin.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flaskpy.web.eternity.admin.entity.Menu;
import com.flaskpy.web.eternity.admin.service.IMenuService;
import com.flaskpy.web.eternity.core.api.R;
import com.flaskpy.web.eternity.core.api.ResponseData;
import com.flaskpy.web.eternity.core.api.SuccessResponse;
import com.flaskpy.web.eternity.core.cache.abs.ICacheOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

/**
 * @className: CacheController
 * @description: 这个人很赖不留下一点说明
 * @author: flaskpy 代码copy大师
 * @date: 2021/6/11
 **/
@RestController
@RequestMapping("/cache")
@Slf4j
public class CacheController {
    @Autowired
    private IMenuService menuService;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    private ICacheOperation<List<Menu>> menuCache;
    @GetMapping("/c1")
    public ResponseData<List<Menu>> c1(){

        List<Menu> result = this.menuCache.get("ppp");
        if(result==null || result.isEmpty()){
            result=this.menuService.list();
            this.menuCache.set("ppp",result,30);
            log.info("数据库中获取");
        }
        else{
            log.info("缓存中获取");
        }
        log.info(Arrays.toString(this.menuCache.keys().toArray()));
        return R.success(result);

    }
    @GetMapping("/c2")
    public ResponseData<List<Menu>> c2(){
        Page<Menu> data = this.menuService.page(new Page<>(3, 5));
        SuccessResponse<List<Menu>> result = R.success(data);
        return result;
    }
}
